package com.reggie.module.controller;

import com.reggie.common.ResultResponseVo;
import com.reggie.common.enums.backend.DishEnum;
import com.reggie.common.enums.backend.SetmealEnum;
import com.reggie.module.entity.dto.setmealDto.SetmealDto;
import com.reggie.module.service.DishService;
import com.reggie.module.service.SetmealDishService;
import com.reggie.module.service.SetmealService;
import com.reggie.module.service.SetmealStatusService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:30:42
 * @Desc:
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
@CrossOrigin
@Validated
@Api(tags = "套餐管理模块")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealStatusService setmealStatusService;


    /**
     * 添加套餐
     *
     * @param setmealDto
     * @param request
     * @return
     */
    @PostMapping
    public ResultResponseVo save(@RequestBody SetmealDto setmealDto, HttpServletRequest request) {
        return setmealService.saveSetmeal(setmealDto, request);
    }

    /**
     * 套餐信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public ResultResponseVo page(int page, int pageSize, String name) {
        return setmealService.selectByPage(page, pageSize, name);
    }

    /**
     * 套餐 起售/停售
     *
     * @param status
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/status/{status}")
    public ResultResponseVo changeStatus(@PathVariable Integer status,
                                         @RequestParam(value = "ids") List<Long> ids,
                                         HttpServletRequest request) {
        int size = ids.size();
        if (size > 10) {
            return SetmealEnum.SIZE_MAX.getResultResponseVo();
        }
        return setmealStatusService.changeStatus(status, ids, request);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResultResponseVo deleteSetmeal(@RequestParam(value = "ids") List<Long> ids) {
        int size = ids.size();
        if (size > 10) {
            return SetmealEnum.SIZE_MAX.getResultResponseVo();
        }
        return setmealService.deleteSetmeal(ids);
    }

    /**
     * 获取套餐列表信息（前台）
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public ResultResponseVo listById(@RequestParam(value = "categoryId") Long categoryId,
                                     @RequestParam(value = "status",required = false) Integer status){
        log.info("categoryId:{},status:{}",categoryId,status);
        return setmealService.getListById(categoryId,status);
    }

    /**
     * 获取套餐中的菜品信息（前台）
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public ResultResponseVo getMealDishDetails(@PathVariable Long id){
        return setmealService.getMealDishDetails(id);
    }

    /**
     * 修改套餐前获取套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultResponseVo getSetmealById(@PathVariable Long id){
        return setmealService.getSetmealById(id);
    }

}
